package libping6;
$VERSION='0.10';
BEGIN {
	if(!exists($::ENV{LINUXHA_LIBPING6_PATH})) {
		die "No LINUXHA_LIBPING6_PATH variable.";
	}
	# Check to see if module exists and if it does not then
	# compile it.
	if(! -f "$::ENV{LINUXHA_LIBPING6_PATH}/libping6.so") {
		my $c="cd $::ENV{LINUXHA_LIBPING6_PATH} && make libping6.so >/dev/null 2>/dev/null";
		system($c);
		if(! -f "$::ENV{LINUXHA_LIBPING6_PATH}/libping6.so") {
			die "Failed to compile libping6.so module.";
		}
	} else {
		;
	}
}

use Time::HiRes qw(time);

use Inline C => DATA =>
	INC => "-I$::ENV{LINUXHA_LIBPING6_PATH}",
	MYEXTLIB => "$::ENV{LINUXHA_LIBPING6_PATH}/libping6.so",
	NAME => 'libping6',
	# VERSION => '0.10',
	TYPEMAPS => "$::ENV{LINUXHA_LIBPING6_PATH}/typemap";

sub new {
my $proto=shift;
my $class=ref($proto) || $proto;
my $self={};
my %args=@_;

	my $t=2.0;
	my $h=undef;
	$t=$args{TIMEOUT} if exists($args{TIMEOUT});
	$h=$args{HOST} if exists($args{HOST});
	$self->{TIMEOUT}=$t;
	$self->{HOST}=$h;

	bless($self,$class);
	return $self;
}

sub ping {
my $self=shift;
my $host=shift;
my $timeout=shift;

	if(!defined($timeout)) {
		$timeout=$self->{TIMEOUT};
	}
	$timeout=5 if !defined($timeout);
	my $res=myping6($host,0,$timeout);
	if($res<0) {
		return 0;
	}
	return 1;
}

sub ping6 {
my $self=shift;
my %args=@_;

	my $t=$self->{TIMEOUT};
	my $h=$self->{HOST};
	$t=$args{TIMEOUT} if exists($args{TIMEOUT});
	$h=$args{HOST} if exists($args{HOST});
	if(!defined($h)) {
		return(-1,"No HOST defined.");
	}

	my $res=myping6($h,0,$t);
	if($res<0) {
		return(-1,myping6_error());
	}
	return ($res,undef);
}

1;

__DATA__
__C__
#include "libping6.h"

float myping6(char *addr,char *dev,float timeout) {
	return libping6_ping(addr,NULL,timeout,0);
}

char *myping6_error() {
	return libping6_ping_errstr();
}

